package com.zeus.controller;

import com.zeus.pojo.Cart;
import com.zeus.pojo.CartInfo;
import com.zeus.pojo.Users;
import com.zeus.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author 冯扬龙
 * @date 2021/4/19 16:19
 */
@Controller
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    //    挑选好商品以后，点击添加到购物车，要发生的一系列操作
    @RequestMapping("/addGoodsToCart")
    @ResponseBody
    public int addGoodsToCart(int skuId, int buyNum, HttpServletRequest request) {
//       首先利用session判断一下用户是否登录了，如果登录了就同步购物车的信息，假如没登录，就返回一个-1，-1就是前端里的msg,之后再alert一个登录窗口
        HttpSession session = request.getSession();
        if (session.getAttribute("loginUser") != null) {
//            假如登录了就从session取出cartid
            Users users = (Users) session.getAttribute("loginUser");
            int cartId = users.getCart().getCartId();
//            拿到cartid以后再把cartid和buynum和skuid组装成cartinfo对象,交给service处理
            CartInfo cartInfo = new CartInfo();
            cartInfo.setBuyNum(buyNum);
            cartInfo.setCartId(cartId);
            cartInfo.setSkuId(skuId);
            //3.交给service层处理
            int totalNum = cartService.addGoodsToCart(cartInfo);
            //根据session中的信息
            Cart cart = users.getCart();
            cart.setTotalNum(totalNum);
            users.setCart(cart);
//            修改过后重新用session存回去
            session.setAttribute("loginUser", users);
            return totalNum;
        } else {
            return -1;
        }
    }

    //    首页点击购物车，进入购物车页面
    @RequestMapping("/showCartList")
    public ModelAndView showCartList(HttpServletRequest request) {
        HttpSession session = request.getSession();
//         先判断用户是否登录
        if (session.getAttribute("loginUser") != null) {
            Users users = (Users) session.getAttribute("loginUser");
            int cartId = users.getCart().getCartId();
            Cart cart = cartService.showCartList(cartId);
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.addObject("cart", cart);
            modelAndView.setViewName("shopcart");
            return modelAndView;
        } else {
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.setViewName("login");
            return modelAndView;
        }
    }

    //    购物车页面，前端点击删除按钮，传回来一个skuid,再到session里面拿到cartid，两个条件确保删除成功
    @RequestMapping("/deleteGoodsFromCart")
    @ResponseBody
    public int deleteGoodsFromCart(int skuId, HttpServletRequest request) {
//        删除之前还是要判断一下用户是否登录
        HttpSession session = request.getSession();
        if (session.getAttribute("loginUser") != null) {
            Users users = (Users) session.getAttribute("loginUser");
            int cartId = users.getCart().getCartId();
            CartInfo cartInfo = new CartInfo();
            cartInfo.setCartId(cartId);
            cartInfo.setSkuId(skuId);
            int totalNum = cartService.deleteGoodsFromCart(cartInfo);
            users.getCart().setTotalNum(totalNum);
            session.setAttribute("loginUser", users);
            return totalNum;
        } else {
            return -1;
        }
    }
}
